'''Deals with annoying text that you don't want to deal with.'''
############################################################################
#    Copyright (C) 2006 by Robert Small                                    #
#    SmallR2002@Gmail.com                                                  #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

__module_name__        = "bcbailiff"
__module_version__     = "0.0.1"
__module_description__ = "Deals with annoying things you don't want to."

import xchat
import bcsettings
import bcmisc
import string #IGNORE:W0402
import re

def scan_text(word, word_eol, userdata): #IGNORE:W0613
    '''
    Procedure which runs through text looking for things to kick for.
    @param word: all the data you need to work with, chopped up into small parts.
    @type word: string
    @param word_eol: all the data you need to work with, chopped from wherever you're starting from.
    @type word_eol: string
    @param userdata: custom object to pass back.
    @type userdata: object
    '''
    for w in bcsettings.bailiff_chans:
        if (xchat.get_info("network") != None) and (xchat.get_info("channel") != None) and len(word) > 1:
            if ((w.split(" ")[0].lower() == xchat.get_info("network").lower()) and (w.split(" ")[1].lower() == xchat.get_info("channel").lower())):
                if (bcsettings.bailiff_checkcaps):
                    if ((len(filter(string.lowercase.__contains__, word[1])) + len(filter(string.whitespace.__contains__, word[1])) + len(filter(string.punctuation.__contains__, word[1]))  + len(filter(string.digits.__contains__, word[1])))*2 < len(filter(string.uppercase.__contains__, word[1]))) and (len(word[1]) > 6):
                        if prekickcheck(bcmisc.clean_text(word[0], True, False)):
                            xchat.command("kick " + bcmisc.clean_text(word[0], True, False) + " Caps! (BottleCap)")
                
                if (bcsettings.bailiff_checkpunctuation):
                    if re.match(bcmisc.punctuation_regexp, word[1]):
                        if prekickcheck(bcmisc.clean_text(word[0], True, False)):
                            xchat.command("kick " + bcmisc.clean_text(word[0], True, False) + " Mind that punctuation... (BottleCap)")
    return xchat.EAT_NONE
        

def prekickcheck(snick):
    '''
    Run a check to make sure you're not doing anything you shouldn't.
    @param snick: the nickname.
    @type snick: string
    '''
    snick = bcmisc.clean_text(snick)
    if (snick not in bcsettings.bailiff_exempts):
        myprefix = "unset"
        
        checklist = xchat.get_list("users")
        for i in checklist:
            if i.nick == xchat.get_info("nick"):
                myprefix = i.prefix
                break
            
        if myprefix == "unset":
            xchat.prnt("Couldn't find ourselves on the list!")
            return False
        
        for i in checklist:
            if i.nick.lower() == snick.lower():
                if  (valuesymbol(myprefix) >= valuesymbol(i.prefix) and valuesymbol(myprefix) >= 3) or (valuesymbol(myprefix) > valuesymbol(i.prefix) and valuesymbol(myprefix) == 2):
                    return True
        
def valuesymbol(symbol):
    '''
    Check the 'height' of a symbol.
    @param symbol: the symbol to check.
    @type symbol: string
    '''
    if symbol == "":
        return 0
    elif symbol == "+":
        return 1
    elif symbol == "%":
        return 2
    elif symbol == "@":
        return 3
    elif symbol == "&":
        return 4
    elif symbol == "~":
        return 5